package searchstring;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;


public class StringBuscaACMDLBuilder extends AbstractStrategyStringBuscaBuilder{
	private static StringBuscaACMDLBuilder instance;

	private static SearchCriteria searchCriteria;
	
	private StringBuscaACMDLBuilder() {

	}

	/**
	 * This method returns the single instance of the class instance
	 * 
	 * @return {@link StringBuscaACMDLBuilder}
	 */
	public static StringBuscaACMDLBuilder getInstance() {
		if (StringBuscaACMDLBuilder.instance == null) {
			StringBuscaACMDLBuilder.instance = new StringBuscaACMDLBuilder();
			
			//String searchMode = "Title:";
			//String searchMode = "ReviewText:";
			String searchMode = "Abstract:";
			//String searchMode = null;
			
			StringBuscaACMDLBuilder.searchCriteria = new SearchCriteria();
			StringBuscaACMDLBuilder.searchCriteria.setMinYear("2003");
			StringBuscaACMDLBuilder.searchCriteria.setMaxYear("2013");
			StringBuscaACMDLBuilder.searchCriteria.setSearchMode(searchMode);
			StringBuscaACMDLBuilder.searchCriteria.setFullTextAvailable(false);
		}
		return StringBuscaACMDLBuilder.instance;
	}

	public String getWebSource() {
		return "dl.acm.org";
	}
	
	/**
	 * 
	 * 
	 * @param search_String
	 * @return
	 */
	public String getURL(String search_String){ 

		String search_String_formatted = null;
		try {
			search_String_formatted = java.net.URLEncoder.encode(search_String, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		SearchCriteria searchCriteria = StringBuscaACMDLBuilder.searchCriteria;
		
		if(searchCriteria.isFullTextAvailable() && search_String_formatted != null){
			search_String_formatted+= "+and+%28FtFlag%3Ayes%29";
		}
		
		return "http://dl.acm.org/results.cfm" +
				"?within="+search_String_formatted+"&CFID=205842387&CFTOKEN=26178299&adv=1&COLL=DL&qrycnt=374909&DL=ACM&Go.x=41&Go.y=10&termzone=all&allofem=&anyofem=&noneofem=&peoplezone=Name&people=&peoplehow=and&keyword=&keywordhow=AND&affil=&affilhow=AND&pubin=&pubinhow=and&pubby=&pubbyhow=OR" +
						"&since_year=" + searchCriteria.getMinYear()+
						"&before_year=" + searchCriteria.getMaxYear()+
						"&pubashow=OR&sponsor=&sponsorhow=AND&confdate=&confdatehow=OR&confloc=&conflochow=OR&isbnhow=OR&isbn=&doi=&ccs=&subj=";
		
		
	}

	public Map<String, String> getMapQuestaoPesquisaStringBuilder() {
		Map<String, String> mapQuestaoPesquisaStringBuilder = new LinkedHashMap<String, String>();

		SearchCriteria searchCriteria = StringBuscaACMDLBuilder.searchCriteria;
		
		for (QuestaoPesquisa questaoPesquisa : StringBuscaLoader.getInstance().getQuestoesPesquisa()) {

			String stringBusca = "";
			String separador = "";
			
			if(searchCriteria.getSearchMode() == null){
				for (TokenStringBusca tokenStringBusca : questaoPesquisa.getTokensStringBusca()) {
					stringBusca+= separador + tokenStringBusca.getStringBuscaSinonimos(false);
					separador = " AND ";
				}
			}else{
				for (TokenStringBusca tokenStringBusca : questaoPesquisa.getTokensStringBusca()) {
					stringBusca+= separador + tokenStringBusca.getStringBuscaSinonimos(false, searchCriteria.getSearchMode());
					separador = " AND ";
				}
			}

			mapQuestaoPesquisaStringBuilder.put(questaoPesquisa.getName(), "("+stringBusca+")");
		}

		return mapQuestaoPesquisaStringBuilder;
	}
}
